﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace IISAdmin
{
    public class SiteCollection : IEnumerator<Site>, IEnumerable<Site>
    {
        internal SiteCollection(ServerManager ServerManager)
        {
            this.ServerManager = ServerManager;
        }

        public Site Add(string serverComment, string serverBindings, string homeDirectory)
        {
            using (DirectoryEntry w3svc = new DirectoryEntry(this.ServerManager.ADsPath + "/w3svc"))
            {
                object websiteId = (object)w3svc.Invoke("CreateNewSite", new object[] { serverComment, new object[] { serverBindings }, homeDirectory });
                return new Site(new DirectoryEntry(this.ServerManager.ADsPath + "/w3svc/" + websiteId), ServerManager);
            }
        }

        public void Remove(string id)
        {
            using (DirectoryEntry w3svc = new DirectoryEntry(this.ServerManager.ADsPath + "/w3svc/" + id))
            {
                w3svc.DeleteTree();
            }
        }

        private ServerManager ServerManager { get; set; }

        public Int32 Count
        {
            get
            {
                using (DirectoryEntry entry = new DirectoryEntry(string.Format("{0}/W3SVC", ServerManager.ADsPath)))
                {
                    int fCount = 0;
                    foreach (DirectoryEntry site in entry.Children)
                    {
                        if (site.SchemaClassName == "IIsWebServer")
                        {
                            fCount++;
                        }
                    }
                    return fCount;
                }
            }
        }

        public Site this[Int32 index]
        {
            get 
            {
                using (DirectoryEntry entry = new DirectoryEntry(string.Format("{0}/W3SVC", ServerManager.ADsPath)))
                {
                    int fCount = 0;
                    foreach (DirectoryEntry site in entry.Children)
                    {
                        if (site.SchemaClassName == "IIsWebServer")
                        {
                            if (fCount == index)
                            {
                                return new Site(site, ServerManager);
                            }
                            fCount++;
                        }
                    }
                }
                throw new Exception("Index out of bounds."); 
            }
        }

        public Site this[string SiteNameOrIdentifier]
        {
            get
            {
                using (DirectoryEntry entry = new DirectoryEntry(string.Format("{0}/W3SVC", ServerManager.ADsPath)))
                {
                    foreach (DirectoryEntry site in entry.Children)
                    {
                        if (site.SchemaClassName == "IIsWebServer")
                        {
                            string ServerComment = site.Properties["ServerComment"].Value.ToString();
                            if (ServerComment == SiteNameOrIdentifier || site.Name == SiteNameOrIdentifier)
                            {
                                return new Site(site, ServerManager);
                            }
                        }
                    }
                }
                throw new Exception("Index out of bounds.");
            }
        }


        private Int32 _Current = -1;

        #region IEnumerator Members

        public object Current
        {
            get { return this[_Current]; }
        }

        public bool MoveNext()
        {
            _Current++;
            if (_Current >= this.Count)
                return false;
            return true;
        }

        public void Reset()
        {
            _Current = -1;
        }

        #endregion

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<Site> Members

        Site IEnumerator<Site>.Current
        {
            get { return this[_Current]; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            // To-Do
        }

        #endregion

        #region IEnumerable<Site> Members

        IEnumerator<Site> IEnumerable<Site>.GetEnumerator()
        {
            return this;
        }

        #endregion
    }
}
